package 剑指Offer.easy;

import java.util.*;

public class 从上到下打印二叉树 {

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    class Solution {
        public int[] levelOrder(TreeNode root) {
            List<Integer> list = new ArrayList<>();
            Deque<TreeNode> queue = new LinkedList<>();
            if (root != null) {
                queue.push(root);
            }
            while (!queue.isEmpty()) {
                int size = queue.size();
                for (int i = 0; i < size; i++) {
                    TreeNode treeNode = queue.pollFirst();
                    list.add(treeNode.val);
                    if (treeNode.left != null) {
                        queue.addLast(treeNode.left);
                    }
                    if (treeNode.right != null) {
                        queue.addLast(treeNode.right);
                    }
                }
            }
            return list.stream().mapToInt(Integer::valueOf).toArray();
        }
    }
}
